package com.lmx.tour_management_system.controller;


import com.lmx.tour_management_system.mapper.CommentsMapper;
import com.lmx.tour_management_system.pojo.AccountTourist;
import com.lmx.tour_management_system.pojo.Comments;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.Date;

/**
 * @author lmx
 * @since 2021-01-25
 */
@Controller
public class CommentsController {

    @Resource
    CommentsMapper commentsMapper;


    /**
     * 添加一条评论
     * @param comment
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/addComment")
    public String addComment(String comment, HttpSession session, Model model){
        AccountTourist touristAccount = (AccountTourist)session.getAttribute("touristAccount");

        if(touristAccount==null){
            model.addAttribute("msg","请登录后再发表评论！");
            return "redirect:/toComments";
        }

        if(comment.trim().equals("")){
            model.addAttribute("msg","请输入评论内容！");
        }

        Comments comments = new Comments(null, comment, touristAccount.getAccountId(), new Date());
        commentsMapper.insert(comments);

        return "redirect:/toComments";

    }

    @RequestMapping("/deleteComment/{id}")
    public String deleteComment(@PathVariable("id") int id){
        commentsMapper.deleteById(id);
        return "redirect:/toCommentsList";
    }

}

